package com.nowcoder.community.event;

import com.alibaba.fastjson.JSONObject;
import com.nowcoder.community.entity.Event;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

/**
 * @BelongsProject: community
 * @BelongsPackage: com.nowcoder.community.event
 * @Author: Master_XXJS
 * @CreateTime: 2023-01-07  14:16
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class EventProducer {

    @Autowired
    private KafkaTemplate kafkaTemplate;

    /**
     * 将 事件转换为json 发送出去
     * 生产者，主动调用
     * @param event 事件对象
     */
    public void fireEvent(Event event){
        //将事件发送到 指定的主题，最好将数据转换为  json 数据
        kafkaTemplate.send(event.getTopic(), JSONObject.toJSONString(event));
    }
}

